<template>
  <div>
    <p class="tip">
      树形虚拟滚动渲染<span class="orange">（最大可以支撑 1w 列、20w 行）</span>，具体兼容请查看 <a class="link" href="https://github.com/x-extends/vxe-table-plugin-virtual-tree/tree/v2" target="_blank">vxe-table-plugin-virtual-tree</a> 插件的 API<br>
      <span class="red">（注：启用纵向虚拟滚启动后不支持动态行高）</span>
    </p>

    <vxe-virtual-tree
      show-overflow
      show-header-overflow
      ref="xVTree"
      height="500"
      :column-config="{resizable: true}"
      :loading="loading"
      :row-config="{useKey: true}"
      :toolbar-config="{slots: {buttons: 'toolbar_buttons'}}"
      :radio-config="{labelField: 'name'}"
      :tree-config="{children: 'children'}">
      <template #toolbar_buttons>
        <vxe-button @click="loadColumnAndData(1000, 5000)">1k列5k条</vxe-button>
        <vxe-button @click="loadColumnAndData(1000, 10000)">1k列1w条</vxe-button>
        <vxe-button @click="loadColumnAndData(1000, 20000)">1k列2w条</vxe-button>
        <vxe-button @click="loadColumnAndData(2000, 10000)">2k列1w条</vxe-button>
        <vxe-button @click="loadColumnAndData(5000, 5000)">5k列5k条</vxe-button>
        <vxe-button @click="loadColumnAndData(5000, 10000)">5k列1w条</vxe-button>
        <vxe-button @click="loadColumnAndData(5000, 20000)">5k列2w条</vxe-button>
        <vxe-button @click="$refs.xVTree.setAllTreeExpand(true)">展开所有</vxe-button>
        <vxe-button @click="$refs.xVTree.setAllTreeExpand(false)">收起所有</vxe-button>
        <vxe-button @click="getRadioEvent">获取选中</vxe-button>
      </template>
    </vxe-virtual-tree>

    <p class="demo-code">{{ $t('app.body.button.showCode') }}</p>

    <pre>
      <pre-code class="xml">{{ demoCodes[0] }}</pre-code>
      <pre-code class="javascript">{{ demoCodes[1] }}</pre-code>
    </pre>
  </div>
</template>

<script>
import VXETable from 'vxe-table'

export default {
  data () {
    return {
      loading: false,
      demoCodes: [
        `
        <vxe-virtual-tree
          show-overflow
          show-header-overflow
          ref="xVTree"
          height="500"
          :column-config="{resizable: true}"
          :loading="loading"
          :row-config="{useKey: true}"
          :toolbar-config="{slots: {buttons: 'toolbar_buttons'}}"
          :radio-config="{labelField: 'name'}"
          :tree-config="{children: 'children'}">
          <template #toolbar_buttons>
            <vxe-button @click="loadColumnAndData(1000, 5000)">1k列5k条</vxe-button>
            <vxe-button @click="loadColumnAndData(1000, 10000)">1k列1w条</vxe-button>
            <vxe-button @click="loadColumnAndData(1000, 20000)">1k列2w条</vxe-button>
            <vxe-button @click="loadColumnAndData(2000, 10000)">2k列1w条</vxe-button>
            <vxe-button @click="loadColumnAndData(5000, 5000)">5k列5k条</vxe-button>
            <vxe-button @click="loadColumnAndData(5000, 10000)">5k列1w条</vxe-button>
            <vxe-button @click="loadColumnAndData(5000, 20000)">5k列2w条</vxe-button>
            <vxe-button @click="$refs.xVTree.setAllTreeExpand(true)">展开所有</vxe-button>
            <vxe-button @click="$refs.xVTree.setAllTreeExpand(false)">收起所有</vxe-button>
            <vxe-button @click="getRadioEvent">获取选中</vxe-button>
          </template>
        </vxe-virtual-tree>
        `,
        `
        import VXETable from 'vxe-table'
        
        export default {
          data () {
            return {
              loading: false
            }
          },
          mounted () {
            this.loadColumn()
            this.loadData(500)
          },
          methods: {
            loadData (size) {
              this.loading = true
              this.getTreeList(size).then(data => {
                this.loading = false
                if (this.$refs.xVTree) {
                  this.$refs.xVTree.loadData(data)
                }
              })
            },
            loadColumn (size) {
              const tableColumn = [
                { type: 'seq', title: '序号', width: 100 },
                { type: 'radio', title: 'Name', width: 300, treeNode: true },
                { field: 'id', title: 'ID', width: 200 }
              ]
              for (let index = 0; index < size; index++) {
                tableColumn.push({
                  field: 'col' + index,
                  title: 'col_' + index,
                  width: 100
                })
              }
              if (this.$refs.xVTree) {
                this.$refs.xVTree.loadColumn(tableColumn)
              }
            },
            getTreeList (size) {
              return new Promise(resolve => {
                // 模拟后台生成树结构
                setTimeout(() => {
                  const len1 = size / 2
                  const len2 = 3
                  const len3 = 2
                  const len4 = 2
                  const len5 = 6
                  const result = []
                  const startIndex = 10000
                  let ketIndex = 0
                  for (let index1 = 0; index1 < len1; index1++) {
                    if (ketIndex >= size) {
                      break
                    }
                    const children1 = []
                    for (let index2 = 0; index2 < len2; index2++) {
                      if (ketIndex >= size) {
                        break
                      }
                      const children2 = []
                      for (let index3 = 0; index3 < len3; index3++) {
                        if (ketIndex >= size) {
                          break
                        }
                        const children3 = []
                        for (let index4 = 0; index4 < len4; index4++) {
                          if (ketIndex >= size) {
                            break
                          }
                          const children4 = []
                          for (let index5 = 0; index5 < len5; index5++) {
                            if (ketIndex >= size) {
                              break
                            }
                            const item5 = {
                              id: startIndex + ketIndex++,
                              name: \`name\${index1}_\${index2}_\${index3}_\${index4}_\${index5}\`,
                              children: []
                            }
                            children4.push(item5)
                          }
                          const item4 = {
                            id: startIndex + ketIndex++,
                            name: \`name\${index1}_\${index2}_\${index3}_\${index4}\`,
                            children: children4
                          }
                          children3.push(item4)
                        }
                        const item3 = {
                          id: startIndex + ketIndex++,
                          name: \`name\${index1}_\${index2}_\${index3}\`,
                          children: children3
                        }
                        children2.push(item3)
                      }
                      const item2 = {
                        id: startIndex + ketIndex++,
                        name: \`name\${index1}_\${index2}\`,
                        children: children2
                      }
                      children1.push(item2)
                    }
                    const item1 = {
                      id: startIndex + ketIndex++,
                      name: \`name\${index1}\`,
                      children: children1
                    }
                    result.push(item1)
                  }
                  resolve(result)
                }, 300)
              })
            },
            getRadioEvent () {
              const selectRow = this.$refs.xVTree.getRadioRecord()
              VXETable.modal.alert(selectRow ? selectRow.name : null)
            }
          }
        }
        `
      ]
    }
  },
  mounted () {
    this.loadColumnAndData(100, 500)
  },
  methods: {
    loadColumnAndData (colSize, rowSize) {
      this.loadColumn(colSize)
      this.loadData(rowSize)
    },
    loadData (size) {
      this.loading = true
      this.getTreeList(size).then(data => {
        this.loading = false
        if (this.$refs.xVTree) {
          this.$refs.xVTree.loadData(data)
        }
      })
    },
    loadColumn (size) {
      const tableColumn = [
        { type: 'seq', title: '序号', width: 100 },
        { type: 'radio', title: 'Name', width: 300, treeNode: true },
        { field: 'id', title: 'ID', width: 200 }
      ]
      for (let index = 0; index < size; index++) {
        tableColumn.push({
          field: 'col' + index,
          title: 'col_' + index,
          width: 100
        })
      }
      if (this.$refs.xVTree) {
        this.$refs.xVTree.loadColumn(tableColumn)
      }
    },
    getTreeList (size) {
      return new Promise(resolve => {
        // 模拟后台生成树结构
        setTimeout(() => {
          const len1 = size / 2
          const len2 = 3
          const len3 = 2
          const len4 = 2
          const len5 = 6
          const result = []
          const startIndex = 10000
          let ketIndex = 0
          for (let index1 = 0; index1 < len1; index1++) {
            if (ketIndex >= size) {
              break
            }
            const children1 = []
            for (let index2 = 0; index2 < len2; index2++) {
              if (ketIndex >= size) {
                break
              }
              const children2 = []
              for (let index3 = 0; index3 < len3; index3++) {
                if (ketIndex >= size) {
                  break
                }
                const children3 = []
                for (let index4 = 0; index4 < len4; index4++) {
                  if (ketIndex >= size) {
                    break
                  }
                  const children4 = []
                  for (let index5 = 0; index5 < len5; index5++) {
                    if (ketIndex >= size) {
                      break
                    }
                    const item5 = {
                      id: startIndex + ketIndex++,
                      name: `name${index1}_${index2}_${index3}_${index4}_${index5}`,
                      children: []
                    }
                    children4.push(item5)
                  }
                  const item4 = {
                    id: startIndex + ketIndex++,
                    name: `name${index1}_${index2}_${index3}_${index4}`,
                    children: children4
                  }
                  children3.push(item4)
                }
                const item3 = {
                  id: startIndex + ketIndex++,
                  name: `name${index1}_${index2}_${index3}`,
                  children: children3
                }
                children2.push(item3)
              }
              const item2 = {
                id: startIndex + ketIndex++,
                name: `name${index1}_${index2}`,
                children: children2
              }
              children1.push(item2)
            }
            const item1 = {
              id: startIndex + ketIndex++,
              name: `name${index1}`,
              children: children1
            }
            result.push(item1)
          }
          resolve(result)
        }, 300)
      })
    },
    getRadioEvent () {
      const selectRow = this.$refs.xVTree.getRadioRecord()
      VXETable.modal.alert(selectRow ? selectRow.name : null)
    }
  }
}
</script>
